package com.scqkxx.gamebox.activity.news.adapter;

import android.app.Activity;
import android.content.ContentValues;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.FunctionCallback;
import com.avos.avoscloud.SaveCallback;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.game.adapter.GameGridAdapter;
import com.scqkxx.gamebox.activity.myself.MyselfEditSignatureActivity;
import com.scqkxx.gamebox.activity.news.NewsDetailActivity;
import com.scqkxx.gamebox.activity.news.NewsMarksActivity;
import com.scqkzqtz.base.library.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/8/15. 09点37分
 * name: 唐师
 * 用途 ： 游戏页面的适配器
 */

public class NewsAdapter extends BaseQuickAdapter<AVObject, BaseViewHolder> {

    public interface OnSwipItemListener {
        void onSwipe(RecyclerView.ViewHolder viewHolder);
    }

    private OnSwipItemListener onSwipItemListener;

    public void setOnSwipItemListener(OnSwipItemListener onSwipItemListener) {
        this.onSwipItemListener = onSwipItemListener;
    }

    private List<AVObject> dataList;

    public NewsAdapter(int layoutResId, List<AVObject> data) {
        super(layoutResId, data);
        dataList = data;
    }

    @Override
    protected void convert(BaseViewHolder helper, AVObject item) {
        helper.setText(R.id.tv_trends_name, item.getString("title"));
        helper.setText(R.id.tv_space_name, item.getString("spaceName") + "/" + item.getString("positionName"));
        helper.setText(R.id.tv_space_user, item.getString("belongUserName"));
        helper.setText(R.id.tv_trends_time, new SimpleDateFormat("yyyy年MM月dd日").format(item.getCreatedAt()));

        RoundedCorners roundedCorners = new RoundedCorners(10);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners)
                .override(300, 300)
                .placeholder(R.mipmap.moren)
                .error(R.mipmap.moren);
        Glide.with(mContext)
                .load(item.getString("avatar"))
                .apply(options)
                .into((ImageView) helper.convertView.findViewById(R.id.iv_space_img));

        //动态详情
        helper.convertView.findViewById(R.id.iv_trends1).setOnClickListener(view -> {
            ContentValues contentValues = new ContentValues();
            contentValues.put("goodsId", item.getInt("goodsId"));
            contentValues.put("newsId", Integer.parseInt(item.getObjectId()));
            ActivityUtil.switchTo(mContext, NewsDetailActivity.class, contentValues);
        });
        //标记列表
        helper.convertView.findViewById(R.id.iv_trends2).setOnClickListener(view -> {
            ContentValues contentValues = new ContentValues();
            contentValues.put("goodsId", item.getInt("goodsId"));
            contentValues.put("newsId", Integer.parseInt(item.getObjectId()));
            ActivityUtil.switchTo(mContext, NewsMarksActivity.class, contentValues);
        });
        //物品详情
        helper.convertView.findViewById(R.id.iv_trends3).setOnClickListener(view -> {
            ContentValues contentValues = new ContentValues();
            contentValues.put("objectId", item.getInt("goodsId") + "");
            contentValues.put("spaceId", item.getInt("spaceId"));
            contentValues.put("belongGroupId", item.getInt("belongGroupId"));
            ActivityUtil.switchTo(mContext, MyselfEditSignatureActivity.class, contentValues);
        });

        helper.convertView.findViewById(R.id.rl_mark).setOnClickListener(view -> {
//            Map<String,Object> map = new HashMap<>();
//            map.put("newsId",Integer.parseInt(item.getObjectId()));
//            int isMark = item.getInt("isMark");
//            map.put("isMark",isMark == 0 ? 1 : 0);
//            AVCloud.callFunctionInBackground("readNews", map, new FunctionCallback<Map<String,Object>>() {
//                @Override
//                public void done(Map<String, Object> object, AVException e) {
//                    String error_code = object.get("error_code")+"";
//                    String msg = object.get("msg")+"";
//                    ToastUtils.showShortToast(mContext,msg);
//                }
//            });

            AVQuery<AVObject> query = AVQuery.getQuery("Marks");
            query.whereEqualTo("belongUserId", item.get("belongUserId"));
            query.whereEqualTo("spaceId", item.get("spaceId"));
            query.whereEqualTo("positionId", item.get("positionId"));
            query.whereEqualTo("goodsId", item.get("goodsId"));
            query.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> avObjects, AVException avException) {
                    if (avException == null) {
                        if (avObjects != null && avObjects.size() > 0) {
                            AVObject avObject = avObjects.get(0);
//                            ToastUtils.showShortToast(mContext, "已标记");
                            int layoutPosition = helper.getLayoutPosition();
                            dataList.remove(layoutPosition);
                            notifyDataSetChanged();
                        } else {
                            AVObject avObject = new AVObject("Marks");
                            avObject.put("belongUserId", item.get("belongUserId"));
                            avObject.put("spaceId", item.get("spaceId"));
                            avObject.put("positionId", item.get("positionId"));
                            avObject.put("goodsId", item.get("goodsId"));
                            avObject.saveInBackground(new SaveCallback() {
                                @Override
                                public void done(AVException e) {
                                    if (e == null) {
//                                        ToastUtils.showShortToast(mContext, "标记成功");
                                        int layoutPosition = helper.getLayoutPosition();
                                        dataList.remove(layoutPosition);
                                        notifyDataSetChanged();
                                        if (onSwipItemListener != null) {
                                            onSwipItemListener.onSwipe(helper);
                                        }
                                    } else {
                                        ToastUtils.showShortToast(mContext, e.getMessage());
                                    }
                                }
                            });
                        }
                    }
                }
            });

//            AVObject avObject = new AVObject("Marks");
//            avObject.put("positionId",item.get("positionId"));
//            avObject.put("goodsId",item.get("goodsId"));
//            avObject.put("isPublic",1);
//            avObject.saveInBackground(new SaveCallback() {
//                @Override
//                public void done(AVException e) {
//                    if (e == null) {
//                        ToastUtils.showShortToast(mContext,"标记成功");
//                    }else {
//                        ToastUtils.showShortToast(mContext,e.getMessage());
//                    }
//                }
//            });
        });

        helper.convertView.findViewById(R.id.rl_close).setOnClickListener(view -> {
            int layoutPosition = helper.getLayoutPosition();
            dataList.remove(layoutPosition);
            notifyDataSetChanged();
            if (onSwipItemListener != null) {
                onSwipItemListener.onSwipe(helper);
            }
        });

    }
}
